package cn.iune.erp.autoentry;

import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.iune.erp.accounting.AccountVoucher;
import cn.iune.erp.accounting.AccountVoucherDetail;
import cn.iune.erp.accounting.IAccountVoucherDetailService;
import cn.iune.erp.accounting.IAccountVoucherService;
import cn.iune.erp.accounting.IAccountingService;
import cn.iune.system.service.ConvertUpMoney;

@Transactional
@Service
public class AutoentryServiceImpl implements IAutoentryService {
	
	@Autowired
	private IAccountVoucherService accountVoucherService;
	@Autowired
	private IAccountVoucherDetailService accountVoucherDetailService;
	
	@Autowired
	private IAccountingService accountingService;
	

	//自动分录
	@Override
    public JSONObject automaticEntry(		
    		String origin,
    		String originName,
    		String originNo,
    		String billType,
    		String billTypeName,
    		int direction,
    		int attachQuantity, //附件数量
    		String currency,//币种
    		String currencyLabel,//币种
    		String comments,
   		
    		JSONArray entries,

			String cUsercode,
			String cUsername,
			String userId,
			String employeeNo,
			String employeeName,
			String enterpriseNo,
			String enterpriseName
    	){

		JSONObject json = new JSONObject();
		
		String systemCode = null;
		int flowType = 1;
		
		int formalFlag = 0;

		AccountVoucher accountVoucher = this.insert(origin, originName, originNo, systemCode, billType, billTypeName, direction, attachQuantity, flowType, comments, entries, formalFlag, cUsercode, cUsername, userId, employeeNo, employeeName, enterpriseNo, enterpriseName);
		if(accountVoucher==null) {
			json.put("code", -2);
			json.put("msg","failure");
			json.put("info","生成会计分录失败！");
			return json;
		}
		
		if(formalFlag==1) {
			//抛转成会计凭证啦，可以提交和审批
			accountingService.accountVoucherConfirm(accountVoucher.getfID(), cUsercode, cUsername, userId, employeeNo, employeeName);
			accountingService.accountVoucherAgreeIt(accountVoucher.getfID(), cUsercode, cUsername, userId, employeeNo, employeeName);
		}
		
		json.put("code", 0);
		json.put("msg","success");
		json.put("vouherNo",accountVoucher.getVoucherNo());
		return json;
	}
		

	//生成分录底稿（可以直接生成会计凭证）
	@Override
    public AccountVoucher insert(		
    		String origin,
    		String originName,
    		String originNo,
    		String systemCode, 
    		String billType,
    		String billTypeName, 
    		int direction,
    		int attachQuantity, //附件数量
    		int flowType, 
    		String comments,
    		JSONArray entries,
    		
    		int formalFlag, //是否直接生成凭证
    		
			String cUsercode,
			String cUsername,
			String userId,
			String employeeNo,
			String employeeName,
			String enterpriseNo,
			String enterpriseName
    	){
		
		//单号
		/*
		Map<String,Object> map = systemCodeService.getBillParam(billType, enterpriseNo);
		String billTypeName = (String) map.get("billTypeName");
		String systemCode = (String) map.get("systemCode");
		int flowType = (int) map.get("flowType");
//		String revOpay = (String) map.get("revOpay");
//		int direction = (int) map.get("direction");
//		String costingCode = (String) map.get("costingCode");
		String autoCode = (String) map.get("autoCode");
//		String origin = (String) map.get("origin");
//		String originName = (String) map.get("originName");
		String billNo = (String) map.get("billNo");
		*/
		
//		AccountVoucher accountVoucher = accountVoucherService.packing(billType, billTypeName, systemCode, flowType, direction, autoCode, origin, originName, billNo, cUsercode, cUsername, userId, enterpriseNo, enterpriseName);
		AccountVoucher accountVoucher = accountVoucherService.packing(origin, originName, originNo, billType, billTypeName, systemCode, flowType, direction, cUsercode, cUsername, userId, enterpriseNo, enterpriseName);
		
		accountVoucher.setEmployeeNo(employeeNo);
		accountVoucher.setEmployeeName(employeeName);
		
//		accountVoucher.setOrigin(origin);
//		accountVoucher.setOriginName(originName);
//		accountVoucher.setOriginNo(originNo);
		
		accountVoucher.setDirection(direction);
		accountVoucher.setAttachQuantity(attachQuantity);
		
		accountVoucher.setComments(comments);
		
		BigDecimal debitAmountSum = BigDecimal.ZERO;       //借方金额
		BigDecimal creditAmountSum = BigDecimal.ZERO;       //贷方金额
		
		for(int i=0;i<entries.size();i++) {
			JSONObject entry = entries.getJSONObject(i);
			
			/////////////////////////////////////////////////////////
			
			int dob = entry.getIntValue("dob");
			
			String captionID = entry.getString("captionID");
			String captionNo = entry.getString("captionNo");//科目编码
			String captionName = entry.getString("captionName");
			String captionFullName = entry.getString("captionFullName");//科目名称
			
			BigDecimal entryAmount = entry.getBigDecimal("entryAmount");   //分录金额
			String summarys = entry.getString("summarys");//摘要

			String dealObject = entry.getString("dealObject");
			String dealObjectLabel = entry.getString("dealObjectLabel");
			String dealObjectID = entry.getString("dealObjectID");
			String dealObjectNo = entry.getString("dealObjectNo");
			String shortName = entry.getString("shortName");
			String dealObjectName = entry.getString("dealObjectName");
			
			BigDecimal debitAmount = BigDecimal.ZERO;   //借方金额
			BigDecimal creditAmount = BigDecimal.ZERO;  //贷方金额
			if(dob==1) {
				//借方
				debitAmount = entryAmount;
			}
			if(dob==-1) {
				//贷方
				creditAmount = entryAmount;
			}
			/////////////////////////////////////////////////////////

			AccountVoucherDetail accountVoucherDetail = accountVoucherDetailService.packing(accountVoucher);
			
			accountVoucherDetail.setDob(dob);
			
			accountVoucherDetail.setCaptionID(captionID);
			accountVoucherDetail.setCaptionNo(captionNo);
			accountVoucherDetail.setCaptionName(captionName);
			accountVoucherDetail.setCaptionFullName(captionFullName);
			
			accountVoucherDetail.setEntryAmount(entryAmount);
			accountVoucherDetail.setSummarys(summarys);
			
			accountVoucherDetail.setDealObject(dealObject);
			accountVoucherDetail.setDealObjectLabel(dealObjectLabel);
			//客户
			if("cus".equals(dealObject)) {
				accountVoucherDetail.setCustomerID(dealObjectID);
				accountVoucherDetail.setCustomerNo(dealObjectNo);
				accountVoucherDetail.setCshortName(shortName);
				accountVoucherDetail.setCustomerName(dealObjectName);
			}
			//供应商
			if("pro".equals(dealObject)) {
				accountVoucherDetail.setProviderID(dealObjectID);
				accountVoucherDetail.setProviderNo(dealObjectNo);
				accountVoucherDetail.setPshortName(shortName);
				accountVoucherDetail.setProviderName(dealObjectName);
			}
			
			accountVoucherDetail.setDebitAmount(debitAmount);
			accountVoucherDetail.setCreditAmount(creditAmount);
			
			accountVoucherDetailService.insert(accountVoucherDetail);
			
			//汇总
			debitAmountSum = debitAmountSum.add(accountVoucherDetail.getDebitAmount());
			creditAmountSum = creditAmountSum.add(accountVoucherDetail.getCreditAmount());
			
			//行数增加
			accountVoucher.setMaxDetailNo(accountVoucher.getMaxDetailNo() + 1);
		}
		
		//写入主表汇总金额
		accountVoucher.setDebitAmount(debitAmountSum);
		accountVoucher.setCreditAmount(creditAmountSum);
		//大写金额，参数被带进来之前已经试算平衡过啦。
		String wordsAmount = ConvertUpMoney.toChinese(debitAmountSum.toString());
		accountVoucher.setWordsAmount(wordsAmount);
		
		accountVoucherService.insert(accountVoucher);
		
		//判断是否直接生成凭证
		if(formalFlag==1) {
			accountingService.formalKJPZ(accountVoucher, cUsercode, cUsername, userId);
		}
		
		return accountVoucher;
	}

	
	
	////////////////////////////////////////////////////////////////
	
	
	

}
